package com.demo.java.OD101_150.OD125;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

/**
 * @author bug菌
 * @Source 公众号：猿圈奇妙屋
 * @des： 【去除多余空格】问题
 * @url： https://blog.csdn.net/weixin_43970743/article/details/145722387
 */
public class OdMain {
    public static void main(String[] args) {
        try {
            Scanner scan = new Scanner(System.in);
            String inputValue = scan.nextLine();  // 输入字符串
            if (inputValue == null || inputValue.length() == 0 || inputValue.length() > 100000) {
                System.out.println("input error");
                return;
            }

            String index = scan.nextLine();  // 输入的索引范围
            String[] splitIndexs = index.split(",");  // 拆分多个索引
            String[] values = new String[splitIndexs.length];
            for (int i = 0; i < splitIndexs.length; ++i) {
                String value = splitIndexs[i];
                String[] idxs = value.split(" ");
                int leftIndex = Integer.parseInt(idxs[0]);
                int rightIndex = Integer.parseInt(idxs[1]);
                values[i] = inputValue.substring(leftIndex, rightIndex + 1);  // 提取对应的子串
            }

            StringBuilder sb = new StringBuilder();
            char[] charValues = inputValue.toCharArray();
            boolean flag = false;
            for (int j = 0; j < charValues.length; ++j) {
                char ch = charValues[j];
                if ('\'' == ch) {
                    flag = !flag;  // 切换标志位，处理单引号内的内容
                }
                if (flag) {
                    sb.append(ch);
                    continue;
                }
                if (ch == ' ' && charValues[j - 1] == ' ') {
                    continue;  // 去掉多个空格
                }
                sb.append(ch);  // 添加字符
            }

            System.out.println(sb);  // 输出处理后的字符串

            // 输出子串索引位置
            for (String value : values) {
                int startIndex = sb.indexOf(value);
                int endIndex = startIndex + value.length() - 1;
                String result = Arrays.toString(new int[]{startIndex, endIndex});
                System.out.print(result);  // 打印对应的索引
            }
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("input error");
        }
    }
}